package com.zhang.controller.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.system.entity.UserRole;
import com.zhang.system.vo.ModifyRoleVO;
import com.zhang.system.vo.UserPermissionVo;
import com.zhang.system.entity.Menu;
import com.zhang.system.entity.RoleMenu;
import com.zhang.system.service.IMenuService;
import com.zhang.system.service.IRoleMenuService;
import com.zhang.system.entity.Role;
import com.zhang.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 关联表：角色与菜单权限表 前端控制器
 * </p>
 *
 * @author 创世神
 * @since 2023-07-06
 */
@Controller
@ResponseBody
@RequestMapping("/permissions/roleMenu/")
@SaCheckLogin
public class RoleMenuController {
    @Autowired
    private IRoleMenuService roleMenuService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;

    @PostMapping("getRolesMenus")
    public R<Map> getPermissionFromUserCurrent(@RequestBody List<Long> roleIds) {
        List<Role> roles = roleService.listByIds(roleIds);
        HashMap<String, Object> map = new HashMap<>();
        List<Menu> menus = roleMenuService.getMenuFromRole(roles);
        List<Menu> menuTree = menuService.getMenuTree(menus);//格式化树结构
        map.put("menuPermission", menuTree);
        return RUtils.successData(map);
    }

    @PostMapping("get/my/menuCodes/by/roleIds")
    public R getMenuCodesFromRoleIds(@RequestBody List<Long> roleIds) {
        List<Menu> menuList = roleMenuService.getMenusFromRoleIds(roleIds);
        List<String> list = menuList.stream().map(item -> {
            return item.getMenuCode();
        }).collect(Collectors.toList());
        return RUtils.successData(list);
    }

    @GetMapping("getMenusFromRole/{roleId}")
    public R getMenusFromRole(@PathVariable Long roleId) {
        List<Menu> list = roleMenuService.getMenuFromRoleId(roleId);
        return RUtils.successData(list);
    }

    @PostMapping("addRoleMenus")
    public R grantRoleToMenus(@RequestBody RoleMenu roleMenu) {
        for (Long menuId : roleMenu.getMenuIds()) {
            roleMenu.setMenuId(menuId);
            roleMenuService.save(roleMenu);
        }
        return RUtils.successMsg("权限授予成功");
    }

    @PostMapping("getRouterInfoFromRole")
    public R<Menu> getRouterInfoFromRole(@RequestBody List<Long> roleIds) {
        List<Menu> menuList = roleMenuService.getRouterInfoFromRole(roleIds);
        return RUtils.successData(menuList);
    }

    /**
     * 获取登录人的权限信息
     *
     * @return {@link R}
     */
    @GetMapping("loginUser")
    public R getPermissionByLoginUser() {
        long userId = StpUtil.getLoginIdAsLong();
        UserPermissionVo userPermissionVo = roleMenuService.getPermissionByUserId(userId);
        return RUtils.successData(userPermissionVo);
    }

    @GetMapping("userId")
    public R getPermissionInfo(@RequestParam String userId) {
        List<Menu> menus = roleMenuService.getPermissionInfoByUserId(Long.valueOf(userId));
        return RUtils.successData(menus);
    }

    @DeleteMapping("{id}")
    public R removeRolePermission(@PathVariable String id) {
        roleMenuService.removeById(id);
        return RUtils.successMsg("删除成功");
    }

    /**获取修改角色的VO
     * @param roleId
     * @return {@link R}
     */
    @GetMapping("modifyRoleId/{roleId}")
    @SaCheckRole("admin")
    R getModifyRoleVO(@PathVariable String roleId) {
        ModifyRoleVO modifyRoleVO = roleMenuService.getModifyRoleVO(roleId);
        return RUtils.successData(modifyRoleVO);
    }

    @PutMapping
    public R modifyRole(@RequestBody ModifyRoleVO modifyRoleVO) {
        roleMenuService.modifyRole(modifyRoleVO);
        return RUtils.successMsg("修改成功!");
    }

}
